package lq231;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*
lq231
题解：https://www.yuque.com/u21257367/kb/hs4ti0r07wv2mz43
输入数据：
10 3
输出：
4
 * */
class Node{
	int val;
	Node next;
	Node prev;
	//初始化
	public Node(int v) {
		val = v;
		next = null;
		prev = null;
	}
}
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int k = sc.nextInt();
		
		/*
		//初始化链表
		Node firstNode = new Node(1);
		//组成环形链表
		firstNode.next = firstNode;
		firstNode.prev = firstNode;
		
		Node curNode = firstNode;
		for(int i=2;i<=n;i++) {
			Node newNode = new Node(i);
			newNode.next = firstNode;
			newNode.prev = curNode;
			curNode.next = newNode;
			firstNode.prev = newNode;
			curNode = newNode;
		}
		
		
		
		int cnt = 1; //计数器
		curNode = firstNode;
		while(curNode.next!=curNode) {
			if(cnt%k==0) {
				//删除当前节点
				curNode.prev.next = curNode.next;
				curNode.next.prev = curNode.prev;
				//重设当前节点
				curNode = curNode.prev;
			}
			//当前节点移动到下一个
			curNode = curNode.next;
			//计数器加1
			cnt++;
		}
		*/
		//输出结果
		System.out.println(lastRemaining(n,k)+1);
	}
	
	/*
	//List实现循环队列
	public static int lastRemaining(int n, int m) {
        if(m==1)
            return n-1;
        List<Integer>list=new ArrayList<>();
        for(int i=0;i<n;i++)
        {
            list.add(i+1);
        }
        int index=0;
        while (list.size()>1)
        {
        	index=(index+m-1)%(list.size());
            list.remove(index);
        }
        return list.get(0);
    }
	*/
	
	/*
	//递推公式计算，递归法
	public static int lastRemaining(int n, int m) {
	    if(n==1)
	        return 0;      
	    return (lastRemaining(n-1,m)+m)%n;
	}
	*/
	
	//递推公式计算，迭代法
	public static int lastRemaining(int n, int m) {
		int value=0;
	    for(int i=1;i<=n;i++){
	        value=(value+m)%i;
	    }
	    return value;
	}

}
